package com.atguigu.mybatisplusdemo.service.impl;

import com.atguigu.mybatisplusdemo.entity.User;
import com.atguigu.mybatisplusdemo.mapper.ExampleMapper;
import com.atguigu.mybatisplusdemo.mapper.UserMapper;
import com.atguigu.mybatisplusdemo.service.UserService;
import com.atguigu.mybatisplusdemo.vo.UserVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;
import java.util.function.Predicate;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private ExampleMapper exampleMapper;

    @Override
    public List<User> findAll() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(null);
        queryWrapper.select("id","name","age");
        queryWrapper.select(User.class, x -> "name".equals(x.getColumn()));
        queryWrapper.select(x -> !"name".equals(x.getColumn()));
        queryWrapper.gt("age",18);
//        queryWrapper.eq("address","狐山");

//        User user = new User();
//        user.setId(100L);
//        user.setName("ALILI");
//        baseMapper.updateById(user);

        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.gt("id",100);
        updateWrapper.set("age",99);
        baseMapper.update(null,updateWrapper);

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.gt(User::getAge,99);
//        lambdaQueryWrapper.eq(User::getAddressStr,"狐山");

        User myUser = baseMapper.findMyUser(1L);

//        baseMapper.findMyUserByWrapper(queryWrapper);

        IPage<User> page = new Page<>(1,2);
        page.setCurrent(1);
        page.setSize(2);
        IPage<User> page1 = baseMapper.selectPage(page, null);
        System.out.println(page.getRecords());
        System.out.println(page.getTotal());
        System.out.println(page.getCurrent());

        Page<User> page2 = new Page<>();
        baseMapper.selectPageVo(page2, null);
        System.out.println(page2.getRecords());

        return baseMapper.selectList(null);
    }

    @Override
    public User test() {
        exampleMapper.selectById(3L);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("",3L);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getName,"三毛");
        return baseMapper.findMyUser(10L);
    }


}
